//
//  DFProductTagWrapView.swift
//  DailyFresh
//
//  Created by Arthur on 2017/9/28.
//  Copyright © 2017年 DailyFresh. All rights reserved.
//

import UIKit
import YYKit

class DFProductTagWrapView: UIView, UICollectionViewDelegate , UICollectionViewDataSource, UICollectionViewDelegateFlowLayout{
    
    var tags: Array<DFProductTag> = []
    lazy var collectionView: UICollectionView = {
        [unowned self] in
        
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .horizontal
        var tmpCollectionView = UICollectionView.init(frame: CGRect.zero, collectionViewLayout: layout);
        tmpCollectionView.backgroundColor =  UIColor.white
        tmpCollectionView.dataSource = self
        tmpCollectionView.delegate = self
//        tmpCollectionView.
        tmpCollectionView.register(DFProductTagCell.self, forCellWithReuseIdentifier: "DFProductTagCell")
        return tmpCollectionView
        }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.addSubview(collectionView)
        self.collectionView.snp.makeConstraints { (make) in
            make.edges.equalTo(self).inset(UIEdgeInsetsMake(0, 0, 0, 0))
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    public func update(tags: Array<DFProductTag>) {
        self.tags = tags
        collectionView.reloadData()
    }
    
    //MARK: CollectionViewDelegate
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return tags.count
    }
    
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        
        let tag: DFProductTag = self.tags[indexPath.row]
        let textsize = (tag.name as NSString).size(for: DFProductTagFont, size: CGSize(width: 200, height: DFProductTagHeight), mode: .byCharWrapping)
        
        let fullWidth: CGFloat
        if isStrEmpty(tag.icon) {
            fullWidth = DFProductTagMargin + DFProductTagSpacing + textsize.width + DFProductTagMargin
        } else {
            fullWidth = DFProductTagMargin + DFProductTagIconWidth + DFProductTagSpacing + textsize.width + DFProductTagMargin
        }
        
            
        
        return CGSize.init(width: fullWidth, height: DFProductTagHeight)
    }
    
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return normalize(value: 8)
    }
    
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        
        return UIEdgeInsetsMake(0, 0, 0, 0)
    }
 
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell:DFProductTagCell = collectionView.dequeueReusableCell(withReuseIdentifier: "DFProductTagCell", for: indexPath) as! DFProductTagCell
        cell.update(tag: self.tags[indexPath.row])
        return cell
        
    }

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        //        let type = self.viewModel.type(section: indexPath.section)
        //        if  type == .community {
        //            let cell: YYTCmtContentCell = collectionView.cellForItem(at: indexPath) as! YYTCmtContentCell
        //            let nextVC = YYTCmtDetailVC.init(dynamicId: Int32((cell.cmt?.dynamicId)!))
        //            self.navigationController?.pushViewController(nextVC, animated: true, hideBottomTabBar: true);
        //
        //        }
        
    }

}



let DFProductTagMargin = normalize(value: 2)
let DFProductTagIconWidth = normalize(value: 10)
let DFProductTagSpacing = normalize(value: 5)
let DFProductTagHeight = normalize(value: 15)
let DFProductTagFont = UIFont.systemFont(ofSize: 10)
class DFProductTagCell: UICollectionViewCell {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor.white
        self.addSubview(icon)
        self.addSubview(name)
        
        self.layer.cornerRadius = normalize(value: 2)
        self.layer.masksToBounds = true
        self.layer.borderColor = DFColorConstants.lightGray.cgColor
        self.layer.borderWidth = 0.5

        icon.snp.makeConstraints { (make) in
            make.left.equalTo(normalize(value: 1))
            make.size.equalTo(CGSize.init(width: DFProductTagIconWidth, height: DFProductTagIconWidth))
            make.centerY.equalTo(self)
        }
        
        name.snp.makeConstraints { (make) in
            make.edges.equalTo(self).inset(UIEdgeInsetsMake(0, DFProductTagMargin + DFProductTagIconWidth + DFProductTagSpacing, 0, DFProductTagMargin))
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    public func update(tag: DFProductTag) {
        self.name.text = tag.name
        
        if isStrEmpty(tag.icon) {
            name.snp.updateConstraints { (make) in
                make.edges.equalTo(self).inset(UIEdgeInsetsMake(0, DFProductTagMargin, 0, DFProductTagMargin))
            }
            icon.isHidden = true
        } else {
            name.snp.updateConstraints { (make) in
                make.edges.equalTo(self).inset(UIEdgeInsetsMake(0, DFProductTagMargin + DFProductTagIconWidth + DFProductTagSpacing, 0, DFProductTagMargin))
            }
            icon.isHidden = false
            icon.setImageWith(URL.init(string: tag.icon), placeholder: nil)

        }
        
        
        
    }
    
    lazy var icon: UIImageView = {
        let tmp = UIImageView()
        
        return tmp
    }()
    
    lazy var name: UILabel = {
        let tmp = UILabel()
        tmp.textColor = DFColorConstants.lightGray
        tmp.numberOfLines = 1
        tmp.font = DFProductTagFont
        tmp.textAlignment = .center
        return tmp
    }()
}
